﻿using SqlSugar;
using Store.Manager.ERP.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Store.Manager.ERP.Infrastructure
{
    public class UnitOfWork : IUnitOfWork
    {
        private readonly ISqlSugarClient _db;
        private bool _isTransactionStarted = false;

        public UnitOfWork(ISqlSugarClient db)
        {
            _db = db;
        }

        public void BeginTransaction(IsolationLevel isolationLevel)
        {
            if (!_isTransactionStarted)
            {
                _db.Ado.BeginTran(isolationLevel);
                _isTransactionStarted = true;
            }
        }

        public void Commit()
        {
            if (_isTransactionStarted)
            {
                _db.Ado.CommitTran();
                _isTransactionStarted = false;
            }
        }

        public void Rollback()
        {
            if (_isTransactionStarted)
            {
                _db.Ado.RollbackTran();
                _isTransactionStarted = false;
            }
        }
    }
}
